﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace dzjc
{
    class WebData
    {

        static readonly string data_url = "https://s-file-2.ykt.cbern.com.cn/zxx/ndrs/resources/tch_material/version/data_version.json";
        static readonly string details_url = "https://s-file-2.ykt.cbern.com.cn/zxx/ndrv2/resources/tch_material/details/{0}.json";
        public readonly static string pdf_url = "https://r1-ndr.ykt.cbern.com.cn/edu_product/esp/assets_document/{0}.pkg/pdf.pdf";


        static WebData()
        {
            //设置SSL/TLS安全通道
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls
                | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
        }


        public static string WebDownString(string url)
        {
            var web = new WebClient() { Encoding = Encoding.UTF8 };
            return web.DownloadString(url);
        }

        /// <summary>
        /// 结构树
        /// </summary>
        /// <returns></returns>
        public static ChildrenItem GetTags()
        {
            string tags_url = "https://s-file-2.ykt.cbern.com.cn/zxx/ndrs/tags/tch_material_tag.json";
            var json = WebDownString(tags_url);
            return JsonConvert.DeserializeObject<ChildrenItem>(json);
        }


        /// <summary>
        /// 数据
        /// </summary>
        /// <returns></returns>
        public static List<Data.Root> Get()
        {
            var data_version = WebDownString(data_url);
            var dataObj = JsonConvert.DeserializeObject<Dictionary<string, string>>(data_version);
            var urls = dataObj["urls"].Split(',');
            return urls.AsParallel().Select(u => WebDownString(u))
                .SelectMany(d => JsonConvert.DeserializeObject<List<Data.Root>>(d)).ToList();
        }

        /// <summary>
        /// 详细数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static Details GetDetails(string id)
        {
            var url = string.Format(details_url, id);
            var json = WebDownString(url);
            var obj = JsonConvert.DeserializeObject<Resources.Root>(json);
            var title = obj.title;
            //var page = obj.custom_properties.preview?.Count;
            var page = obj.ti_items.FirstOrDefault(t => t.lc_ti_format == "image/jpg")
                ?.custom_properties.requirements.FirstOrDefault(r => r.name == "pagesize")
                ?.value;
            var pdf = string.Format(pdf_url, id);
            //var pdf = obj.ti_items.FirstOrDefault(t => t.ti_format == "pdf")?.ti_storage;  //需要登录才能下载
            var jpg = obj.custom_properties.preview?["Slide1"];
            return new Details { Title = title, Page = Convert.ToInt32(page), Jpg = jpg, Pdf = pdf };
        }
    }

    public class Details
    {
        public string Title { get; set; }
        public int Page { get; set; }
        public string Jpg { get; set; }
        public string Pdf { get; set; }
    }



}
